package com.zhang.teachingcommunity_spring.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.teachingcommunity_spring.excption.businessSystemException;
import com.zhang.teachingcommunity_spring.mapper.CourseMapper;
import com.zhang.teachingcommunity_spring.mapper.SchoolMapper;
import com.zhang.teachingcommunity_spring.mapper.ScoreMapper;
import com.zhang.teachingcommunity_spring.mapper.entity.Course;
import com.zhang.teachingcommunity_spring.mapper.entity.Score;
import com.zhang.teachingcommunity_spring.mapper.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ScoreService {
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private CourseMapper courseMapper;

    public IPage<Score> findAll(Score score, Page Ipage) {
        QueryWrapper<Score> userQueryWrapper = new QueryWrapper<>();
        if(score.getSchoolId() != null && !(score.getSchoolId().equals(""))) {
            userQueryWrapper.eq("school_id", score.getSchoolId());
        }
        if(score.getCourseId() != null && !(score.getCourseId() .equals(""))) {
            userQueryWrapper.eq("course_id", score.getCourseId() );
        }
        if(score.getStuNo() != null && !(score.getStuNo().equals(""))) {
            userQueryWrapper.eq("stu_no", score.getStuNo());
        }
        IPage<Score> page = new Page<>(Ipage.getCurrent(),Ipage.getSize());
        IPage<Score> ScoreIPage = scoreMapper.selectPage(page,userQueryWrapper);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ScoreIPage.getRecords().forEach(item -> {

            Integer courseId = item.getCourseId();
            Course course = courseMapper.selectById(courseId);
            item.setCourses(course);
        });
        return ScoreIPage;
    }

}
